/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import componenten.Database;
import javax.swing.JOptionPane;
import model.Persoon;

/**
 *
 * @author Zeyd
 */
public class PersoonToevoegen extends javax.swing.JFrame {
  Persoon eenPersoon = new Persoon();

      
    /**
     * Creates new form PersoonToevoegen
     */
      public PersoonToevoegen() {
        initComponents();
      //database verbinding maken, hier je database gegevens invullen!  
      if (!Database.openConnection("fullhouse", "root", "")) {
        
      System.out.println("Geen verbinding met de database");
      System.exit(0);
    }
 //   ComboBox.removeAllItems();
    ComboBox.addItem(Persoon.Lid);
    ComboBox.addItem(Persoon.Master);
    
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        Voornaam = new javax.swing.JTextField();
        Achternaam = new javax.swing.JTextField();
        Tussenvoegsel = new javax.swing.JTextField();
        Geboortedatum = new javax.swing.JTextField();
        Email = new javax.swing.JTextField();
        Telefoon = new javax.swing.JTextField();
        Woonplaats = new javax.swing.JTextField();
        Straat = new javax.swing.JTextField();
        Huisnummer = new javax.swing.JTextField();
        Postcode = new javax.swing.JTextField();
        persoonAanmaakButton = new javax.swing.JButton();
        ComboBox = new javax.swing.JComboBox();
        MasterBijnaam = new javax.swing.JTextField();
        MasterLbl = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Voornaam:");

        jLabel2.setText("Achternaam:");

        jLabel3.setText("Tussenvoegsel:");

        jLabel4.setText("Geboortedatum:");

        jLabel5.setText("Email:");

        jLabel6.setText("Telefoon:");

        jLabel7.setText("Woonplaats:");

        jLabel8.setText("Straat:");

        jLabel9.setText("Huisnummer:");

        jLabel10.setText("Postcode:");

        Geboortedatum.setText("yyyy-MM-dd");

        Telefoon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TelefoonActionPerformed(evt);
            }
        });

        Straat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                StraatActionPerformed(evt);
            }
        });

        persoonAanmaakButton.setText("Verstuur");
        persoonAanmaakButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                persoonAanmaakButtonActionPerformed(evt);
            }
        });

        ComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ComboBoxActionPerformed(evt);
            }
        });

        MasterLbl.setText("Master Bijnaam:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9)
                    .addComponent(jLabel10)
                    .addComponent(MasterLbl))
                .addGap(50, 50, 50)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Voornaam)
                    .addComponent(Achternaam)
                    .addComponent(Tussenvoegsel)
                    .addComponent(Geboortedatum)
                    .addComponent(Email)
                    .addComponent(Telefoon)
                    .addComponent(Woonplaats)
                    .addComponent(Straat)
                    .addComponent(Huisnummer)
                    .addComponent(Postcode, javax.swing.GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE)
                    .addComponent(persoonAanmaakButton)
                    .addComponent(MasterBijnaam))
                .addContainerGap(65, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(Voornaam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(Achternaam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(Tussenvoegsel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(Geboortedatum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(Email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(Telefoon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(Woonplaats, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(Straat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(Huisnummer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(Postcode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(MasterBijnaam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(MasterLbl))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(persoonAanmaakButton)
                .addContainerGap(99, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void TelefoonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TelefoonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_TelefoonActionPerformed

    private void StraatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StraatActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_StraatActionPerformed

    private void persoonAanmaakButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_persoonAanmaakButtonActionPerformed
        //lees alle waarden en stop ze in eenStudent 
        eenPersoon.setPersoon_voornaam(Voornaam.getText());
        eenPersoon.setPersoon_achternaam(Achternaam.getText());
        eenPersoon.setPersoon_tussenvoegsel(Tussenvoegsel.getText());
        eenPersoon.setPersoon_geboortedatum(Geboortedatum.getText());
        eenPersoon.setPersoon_email(Email.getText());
        eenPersoon.setPersoon_telefoon(Integer.parseInt(Telefoon.getText()));
        eenPersoon.setPersoon_woonplaats(Woonplaats.getText());
        eenPersoon.setPersoon_straat(Straat.getText());
        eenPersoon.setPersoon_huisnummer(Huisnummer.getText());
        eenPersoon.setPersoon_postcode(Postcode.getText());
        String persoon_soort = ComboBox.getSelectedItem().toString();
        eenPersoon.setPersoon_soort(persoon_soort);
        eenPersoon.setMaster_bijnaam(MasterBijnaam.getText());
        String Voornaam1 = this.Voornaam.getText();
        String Achternaam1 = this.Achternaam.getText();
        String Geboortedatum1 = this.Geboortedatum.getText();
        String Email1 = this.Email.getText();
        String Telefoon1 = this.Telefoon.getText();
        String Woonplaats1 = this.Woonplaats.getText();
        String Straat1 = this.Straat.getText();
        String Huisnummer1 = this.Huisnummer.getText();
        String Postcode1 = this.Postcode.getText();
        if (!Voornaam1.matches("^([A-Za-z]+( [A-Za-z]+)?)$")) {
            JOptionPane.showMessageDialog(null, "Er klopt iets niet bij naam.");
        } else if (!Achternaam1.matches("^([A-Za-z]+( [A-Za-z]+)?)$")) {
            JOptionPane.showMessageDialog(null, "Er klopt iets niet bij achternaam.");
        } else if (!Geboortedatum1.matches("^[0-9]{4}-[0-9]{2}-[0-9]{2}$")) {
            JOptionPane.showMessageDialog(null, "geboortedatum klopt niet");
        } else if (!Email1.matches("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$")) {
            JOptionPane.showMessageDialog(null, "email klopt niet");
        } else if (!Telefoon1.matches("^[0-9]{10}$")) {
            JOptionPane.showMessageDialog(null, "telefoonnummer klopt niet");
        } else if (!Woonplaats1.matches("^([A-Za-z]+( [A-Za-z]+)?)$")) {
            JOptionPane.showMessageDialog(null, "woonplaats klopt niet");
        } else if (!Straat1.matches("^([A-Za-z]+( [A-Za-z]+)?)$")) {
            JOptionPane.showMessageDialog(null, "straat klopt niet");
        } else if (!Huisnummer1.matches("^([0-9]{1,}+([A-Za-z]+)?)$")) {
            JOptionPane.showMessageDialog(null, "huisnummer klopt niet");
        } else if (!Postcode1.matches("^([0-9]{4}+([A-Za-z]{2}+)?)$")) {
            JOptionPane.showMessageDialog(null, "postcode klopt niet");
        } else {



            //sla de gegevens op in de database
            if (eenPersoon.insert()) {
                this.dispose();
            }
        }
    }//GEN-LAST:event_persoonAanmaakButtonActionPerformed

    private void ComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ComboBoxActionPerformed
        switch (ComboBox.getSelectedItem().toString()) {
            case Persoon.Lid:
                MasterBijnaam.setVisible(false);
                MasterLbl.setVisible(false);
                break;
            case Persoon.Master:

                MasterBijnaam.setVisible(true);
                MasterLbl.setVisible(true);
                String MasterBijnaam1 = this.MasterBijnaam.getText();
                if (!MasterBijnaam1.matches("^([A-Za-z]+( [A-Za-z]+)?)$")) {
                    JOptionPane.showMessageDialog(null, "Er klopt iets niet bij master bijnaam.");
                }
                break;
        }  
    }//GEN-LAST:event_ComboBoxActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PersoonToevoegen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PersoonToevoegen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PersoonToevoegen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PersoonToevoegen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new PersoonToevoegen().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField Achternaam;
    private javax.swing.JComboBox ComboBox;
    private javax.swing.JTextField Email;
    private javax.swing.JTextField Geboortedatum;
    private javax.swing.JTextField Huisnummer;
    private javax.swing.JTextField MasterBijnaam;
    private javax.swing.JLabel MasterLbl;
    private javax.swing.JTextField Postcode;
    private javax.swing.JTextField Straat;
    private javax.swing.JTextField Telefoon;
    private javax.swing.JTextField Tussenvoegsel;
    private javax.swing.JTextField Voornaam;
    private javax.swing.JTextField Woonplaats;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JButton persoonAanmaakButton;
    // End of variables declaration//GEN-END:variables
}
